package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbBorrowMapper;
import com.bwie.mapper.TbDictMapper;
import com.bwie.mapper.TbSaveMapper;
import com.bwie.pojo.TbAsset;
import com.bwie.pojo.TbBorrow;
import com.bwie.pojo.TbDict;
import com.bwie.pojo.TbSave;
import com.bwie.service.TbAssetService;
import com.bwie.mapper.TbAssetMapper;
import com.bwie.utils.R;
import com.bwie.utils.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author Yoo19
* @description 针对表【tb_asset】的数据库操作Service实现
* @createDate 2025-07-03 09:45:07
*/
@Service
public class TbAssetServiceImpl extends ServiceImpl<TbAssetMapper, TbAsset>
    implements TbAssetService{
    @Autowired
    private TbAssetMapper tbAssetMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private TbSaveMapper tbSaveMapper;
    @Autowired
    private TbBorrowMapper tbBorrowMapper;
    @Autowired
    private TbDictMapper tbDictMapper;

    @Override
    public R getAssetList(Integer userId) {

        //根据token获取登陆的用户ID
        String token = request.getHeader("token");
        if(token == null){
            return R.ERROR(500,"请先登陆");
        }
        //解析token
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        userId = claims.get("userId", Integer.class);

        //根据id查询用户总资产信息

        List<TbAsset> assetList = tbAssetMapper.getAssetList(userId);
        return R.OK(assetList);
    }

    @Override
    public Result getSaveList(Integer userId) {
        //根据token获取登陆的用户ID
        String token = request.getHeader("token");
        if(token == null){
            return Result.error().setMsg("请先登陆");
        }
        //解析token
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        userId = claims.get("userId", Integer.class);

        //根据id查询用户总资产信息

        List<TbSave> saveList = tbSaveMapper.getSaveList(userId);
        return Result.ok().setData(saveList);

    }

    @Override
    public R getBorrowList(Integer userId) {
        //根据token获取登陆的用户ID
        String token = request.getHeader("token");
        if(token == null){
            return R.ERROR(500,"请先登陆");
        }
        //解析token
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        userId = claims.get("userId", Integer.class);

        //根据id查询用户总资产信息

        List<TbBorrow> borrowList = tbBorrowMapper.getBorrowList(userId);
        return R.OK(borrowList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result tx(TbSave tbSave, TbAsset tbAsset) {
        return null;
    }

    @Override
    public R getCardList(Integer typeId) {
        List<TbDict> cardList = tbDictMapper.selectList(new LambdaQueryWrapper<TbDict>().eq(TbDict::getDictType, typeId));
        return R.OK(cardList);
    }


}




